const mysql = require('../server/node_modules/mysql2/promise');

async function insertTestData() {
    let connection;

    try {
        // 创建数据库连接
        connection = await mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: '159680',
            database: 'juequ_zero_db'
        });

        console.log('数据库连接成功');

        // 插入测试用户
        const testUser = {
            user_id: 'test_user_001',
            username: 'testuser',
            password: 'hashed_password',
            nickname: '测试用户',
            avatar: 'images/avatar1.png',
            gender: 1,
            phone: '13800138000',
            email: 'test@example.com',
            game_uid: '12345678'
        };

        try {
            await connection.query(
                `INSERT INTO t_user (user_id, username, password, nickname, avatar, gender, phone, email, game_uid) 
                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
                [testUser.user_id, testUser.username, testUser.password, testUser.nickname, 
                 testUser.avatar, testUser.gender, testUser.phone, testUser.email, testUser.game_uid]
            );
            console.log('测试用户插入成功');
        } catch (error) {
            if (error.code === 'ER_DUP_ENTRY') {
                console.log('测试用户已存在，跳过插入');
            } else {
                throw error;
            }
        }

        // 插入测试帖子数据
        const testPosts = [
            {
                post_id: 'post_001_' + Date.now(),
                title: '新手攻略：如何快速上手绝区零',
                content: '这是一篇详细的新手攻略，帮助新玩家快速了解游戏机制...',
                post_type: '攻略',
                category: '角色',
                user_id: testUser.user_id,
                view_count: 156,
                like_count: 23,
                comment_count: 8
            },
            {
                post_id: 'post_002_' + Date.now(),
                title: '讨论：大家觉得哪个角色最强？',
                content: '最近玩了几个角色，想和大家讨论一下哪个角色在PVE中最强...',
                post_type: '讨论',
                category: '角色',
                user_id: testUser.user_id,
                view_count: 89,
                like_count: 15,
                comment_count: 12
            },
            {
                post_id: 'post_003_' + Date.now(),
                title: '最新版本更新内容解析',
                content: '本次更新带来了很多新内容，包括新角色、新装备等...',
                post_type: '资讯',
                category: '装备',
                user_id: testUser.user_id,
                view_count: 234,
                like_count: 45,
                comment_count: 18
            },
            {
                post_id: 'post_004_' + Date.now(),
                title: '副本攻略：深渊挑战技巧分享',
                content: '分享一些深渊挑战的技巧和心得，希望对大家有帮助...',
                post_type: '攻略',
                category: '副本',
                user_id: testUser.user_id,
                view_count: 178,
                like_count: 32,
                comment_count: 14
            },
            {
                post_id: 'post_005_' + Date.now(),
                title: '剧情讨论：主线故事深度解析',
                content: '对主线剧情进行深度解析，探讨游戏的世界观设定...',
                post_type: '讨论',
                category: '剧情',
                user_id: testUser.user_id,
                view_count: 67,
                like_count: 19,
                comment_count: 6
            },
            {
                post_id: 'post_006_' + Date.now(),
                title: '装备搭配推荐：最强输出配置',
                content: '推荐几套强力的装备搭配，适合不同角色使用...',
                post_type: '攻略',
                category: '装备',
                user_id: testUser.user_id,
                view_count: 145,
                like_count: 28,
                comment_count: 11
            },
            {
                post_id: 'post_007_' + Date.now(),
                title: '游戏视频：精彩战斗集锦',
                content: '分享一些精彩的战斗视频，展示游戏的操作技巧...',
                post_type: '视频',
                category: '角色',
                user_id: testUser.user_id,
                view_count: 312,
                like_count: 67,
                comment_count: 25
            },
            {
                post_id: 'post_008_' + Date.now(),
                title: '社区活动：玩家作品征集',
                content: '举办社区活动，征集玩家的创意作品和攻略分享...',
                post_type: '资讯',
                category: '剧情',
                user_id: testUser.user_id,
                view_count: 98,
                like_count: 21,
                comment_count: 9
            }
        ];

        for (const post of testPosts) {
            try {
                await connection.query(
                    `INSERT INTO t_post (post_id, title, content, post_type, category, user_id, view_count, like_count, comment_count) 
                     VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
                    [post.post_id, post.title, post.content, post.post_type, post.category, 
                     post.user_id, post.view_count, post.like_count, post.comment_count]
                );
                console.log(`帖子 "${post.title}" 插入成功`);
            } catch (error) {
                if (error.code === 'ER_DUP_ENTRY') {
                    console.log(`帖子 "${post.title}" 已存在，跳过插入`);
                } else {
                    console.error(`插入帖子失败:`, error.message);
                }
            }
        }

        console.log('测试数据插入完成');
    } catch (error) {
        console.error('插入测试数据失败:', error);
    } finally {
        if (connection) {
            await connection.end();
        }
    }
}

insertTestData();